package com.download;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.getBingJson.GetBingJson;

public class EveryDayDownload {
	
	public static void main(String args[]) throws InterruptedException {
		GetBingJson gbj=new GetBingJson();
		DownLoadPic dp=new DownLoadPic(); 
		long day=24*60*60*1000;
		long runtime=getTimeMillis("21:06:00")-System.currentTimeMillis();
		runtime=runtime>0?runtime:runtime+day;
		ScheduledExecutorService scheduledThreadPool=Executors.newScheduledThreadPool(10);
		scheduledThreadPool.scheduleAtFixedRate (new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				String json=gbj.getBingJson(0,1);
				String url=gbj.getBingUrl(json);
				String name=gbj.getBingName(json);
				dp.downloadPic(url,name);
			}
		},runtime,day, TimeUnit.MILLISECONDS);
	}
	
	private static long getTimeMillis(String time) {
		SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		SimpleDateFormat sdf2=new SimpleDateFormat("yy-MM-dd");
		Date day = null;
		try {
			day=sdf.parse(sdf2.format(new Date())+" "+time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(day.getTime());
		return day.getTime();
	}
}	
